﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class SlopeIndependentCommond : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			Transaction transaction = new Transaction(base.Doc, "添加共享参数");
			transaction.Start();
			MarkOperation markOperation = new MarkOperation();
			markOperation.AddAllShareParametes(base.UiApp);
			transaction.Commit();
			markOperation.SetActiveDocAllLoadedTags(base.Doc, false);
			Transaction transaction2 = new Transaction(base.Doc, "坡度标注");
			if (base.Doc.ActiveView is View3D && !((View3D)base.Doc.ActiveView).IsLocked)
			{
				MessageBox.Show("请锁定3D视图", "鸿业提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (SlopeIndependentCommond.hWndRevit == null)
			{
				SlopeIndependentCommond.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			this.frm = new FrmSlopeIndependent(SlopeIndependentCommond.hWndRevit);
			this.frm.Show(SlopeIndependentCommond.hWndRevit);
			Result result;
			for (;;)
			{
				try
				{
					transaction2.Start();
					new SlopeIndependent(this.jig, this.frm, base.Doc, base.Sel, base.UiApp, base.UiDoc).SetJigAndLayTag();
					transaction2.Commit();
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					if (transaction2 != null && (int)transaction2.GetStatus() == 1)
					{
						transaction2.RollBack();
					}
					this.frm.Close();
					this.frm.Dispose();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex)
				{
					if (transaction2 != null && (int)transaction2.GetStatus() != 3)
					{
						transaction2.RollBack();
					}
					this.frm.Close();
					MessageBox.Show(ex.Message, "鸿业提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					result = Autodesk.Revit.UI.Result.Failed;
				}
				break;
			}
			return result;
		}

		public static YJKWindowHandle hWndRevit;

		private FrmSlopeIndependent frm;

		private XArrowJig jig;
	}
}
